// 刷新内容
function refreach(){
    var kids = $('h2.kindname');
    var kindblock =$('div.kindblock');
    for(var i=0;i<kids.length;i++){
        kid = parseInt($(kids.get(i)).attr('kid'));
        var data = selectGoodsCountsByKid(kid);
        // console.log(data);
        var goodslist = $('<div></div>').attr('class', 'goodslist');
        goodslist.css({'margin': '2px auto','width':'95%'});

        $.each(data, function(index, val) {

            var goodsdisplay = $("<div></div>").attr({'class': 'goodsdisplay','pid' : val.pid});


            var name=$("<h4></h4>").text(val.name).attr('class', 'name hidden');
            var comments = $('<p></p>').attr('class','comments hidden');
            if(val.comments.search('｜')!=-1){
                comments.text(val.comments.split('｜')[0]);
            }
            var oldprice = $('<span></span>').text(val.oldprice+'元').attr('class', 'oldprice');
            var newprice = $('<span></span>').text(val.newprice+'元').attr('class', 'newprice');
            var img = $('<img>').attr({'src':val.imgpath,'class':'goodsimg','title':val.name});
           	// 图片加载失败的时候
            $("img").on("error", function () {
            	  $(this).attr("src", "https://i1.mifile.cn/f/i/2014/cn/placeholder-40.png");
            	});
            img.css({'width': '160px','height':'115px','margin':'40px auto'},);
            var status=$('<span></span>').attr('class', 'status');
            if(val.status==1){
                status.text('在售');
                status.css('color', '#ff6700');
            }else{
                status.text('售完');
                status.css('color', '#b0b0b0');
            }
            goodsdisplay.append(img,name,comments,newprice,oldprice,status);
            goodsdisplay.bind('click', function(event) {
                window.open('http://localhost:8080/mimall/details.do?pid='+val.pid,'_self');
            });
            // goodsdisplay.attr('href', 'http://localhost:8080/mimall/details.do?pid='+val.pid);
            goodslist.append(goodsdisplay);
        });
        $(kindblock.get(i)).append(goodslist);
    }

}

// 根据获得kid来查询相应种类的商品
function selectGoodsCountsByKid(kid){
    var result= '';
    $.ajax({
        url: 'http://localhost:8080/mimall/selectGoodsByParamContainsKid.do',
        type: 'POST',
        async:false,
        data: {'kid': kid},
        timeout: 3000,
    })
    .done(function(callback) {
        console.log("success");
        result = callback;
    })
    .fail(function() {
        console.log("error");
    })
    .always(function() {
        console.log("complete");
    });
    return result;
}